package cn.iocoder.yudao.module.call.controller.admin.cmsipgateway;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.call.controller.admin.cmsipgateway.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.cmsipgateway.CmSipGatewayDO;
import cn.iocoder.yudao.module.call.service.cmsipgateway.CmSipGatewayService;

@Tag(name = "管理后台 - 网关配置")
@RestController
@RequestMapping("/call/cm-sip-gateway")
@Validated
public class CmSipGatewayController {

    @Resource
    private CmSipGatewayService cmSipGatewayService;

    @PostMapping("/create")
    @Operation(summary = "创建网关配置")
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:create')")
    public CommonResult<Long> createCmSipGateway(@Valid @RequestBody CmSipGatewaySaveReqVO createReqVO) {
        return success(cmSipGatewayService.createCmSipGateway(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新网关配置")
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:update')")
    public CommonResult<Boolean> updateCmSipGateway(@Valid @RequestBody CmSipGatewaySaveReqVO updateReqVO) {
        cmSipGatewayService.updateCmSipGateway(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除网关配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:delete')")
    public CommonResult<Boolean> deleteCmSipGateway(@RequestParam("id") Long id) {
        cmSipGatewayService.deleteCmSipGateway(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得网关配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:query')")
    public CommonResult<CmSipGatewayRespVO> getCmSipGateway(@RequestParam("id") Long id) {
        CmSipGatewayDO cmSipGateway = cmSipGatewayService.getCmSipGateway(id);
        return success(BeanUtils.toBean(cmSipGateway, CmSipGatewayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得网关配置分页")
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:query')")
    public CommonResult<PageResult<CmSipGatewayRespVO>> getCmSipGatewayPage(@Valid CmSipGatewayPageReqVO pageReqVO) {
        PageResult<CmSipGatewayDO> pageResult = cmSipGatewayService.getCmSipGatewayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CmSipGatewayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出网关配置 Excel")
    @PreAuthorize("@ss.hasPermission('call:cm-sip-gateway:export')")
    @OperateLog(type = EXPORT)
    public void exportCmSipGatewayExcel(@Valid CmSipGatewayPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmSipGatewayDO> list = cmSipGatewayService.getCmSipGatewayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "网关配置.xls", "数据", CmSipGatewayRespVO.class,
                BeanUtils.toBean(list, CmSipGatewayRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得网关配置")
    @OperateLog(enable = false)
    public CommonResult<List<CmSipGatewayRespVO>> getSipGatewayList() {
        List<CmSipGatewayDO> sipGatewayList = cmSipGatewayService.getSipGatewayList();
        return success(BeanUtils.toBean(sipGatewayList, CmSipGatewayRespVO.class));
    }
}